package Connection;

import java.sql.*;

public class Conexao {

    private Connection conexao;
    private static Conexao instanciaConexao;

    public synchronized Connection getConnection() throws Exception {
        boolean result = true;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_banco", "root", "murilo");
        } catch (ClassNotFoundException e) {
            throw new Exception("O Driver de conexao com o SGBD não esta instalado");
        } catch (Exception e) {
            throw new Exception("Nao foi possivel estabeler conexao com o SGBD");
        }
        return conexao;
    }

    public synchronized static Conexao getInstancia() {
        if (instanciaConexao == null) {
            instanciaConexao = new Conexao();
        }
        return instanciaConexao;
    }

    public synchronized void  closeConnection(Connection conn,
            Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
